package com.gjzhao.fmmall.controller;

import com.gjzhao.fmmall.entity.Orders;
import com.gjzhao.fmmall.service.OrderService;
import com.gjzhao.fmmall.vo.RespResultVO;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description: 订单添加接口
 * @Author: gjzhao
 * @Date Created in 2022-02-08 5:36 下午
 */
@RestController
@RequestMapping("/orders")
@CrossOrigin
@Api(tags = "订单管理", value = "订单处理接口")
public class OrderController extends BaseController {


    @Autowired
    private OrderService orderService;

    @PostMapping("/add")
    @ApiOperation(value = "添加订单")
    @ApiImplicitParam(dataType = "string", name = "cartIds", value = "购物车ids", required = true)
    @HystrixCommand(fallbackMethod = "addFallBack",
        commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000") //如果超过1s还未调通服务，将进行服务降级(一定不能大于服务消费者的降级规定时间)
        }
    )
    public RespResultVO add(@RequestBody Orders order,
                            String cartIds) {

        RespResultVO resultVO = null;
        try {
            Thread.sleep(5000);
            resultVO = orderService.addOrder(cartIds, order);
        } catch (Exception e) {
            logger.info("-----提交订单失败:[" + e.getMessage() + "]-----");
            resultVO = RespResultVO.failed("发生异常，提交订单失败");
        }
        return resultVO;
    }

    // 降级方法
    public RespResultVO addFallBack(@RequestBody Orders order, String cartIds) {

        logger.info("-----调用order-add-hystrix服务超时，进行服务降级------");
        logger.info("-----add降级[服务提供者降级]----");
        return RespResultVO.failed("服务提供者处理异常，添加订单失败，请稍后再试");
    }

    @GetMapping("/getOrder")
    @ApiOperation(value = "查询订单")
    @ApiImplicitParam(dataType = "string", name = "orderId", value = "订单id", required = true)
    @HystrixCommand(fallbackMethod = "getOrderFallBack",
        commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000") //如果超过1s还未调通服务，将进行服务降级
        }
    )
    public RespResultVO getOrder(String orderId) {


        RespResultVO resultVO = null;
        try {
            Thread.sleep(5000);
            resultVO = orderService.getOrder(orderId);
        } catch (Exception e) {
            logger.info("-----查询订单失败:[" + e.getMessage() + "]-----");
            resultVO = RespResultVO.failed("发生异常，查询订单失败");
        }
        return resultVO;
    }


    // 降级方法
    public RespResultVO getOrderFallBack(String orderId) {

        logger.info("-----调用order-add-hystrix服务超时，进行服务降级------");
        logger.info("-----getOrder降级[服务提供者降级]----");
        return RespResultVO.failed("服务提供者处理异常，查询订单失败，请稍后再试");

    }

}
